import GameLauncher from "../GameLauncher";
import { Constants } from "./Constants";

export namespace util{

    export interface Vector3Like{
        x:number,
        y:number,
        z:number,
    }

    export interface Vector2Like{
        x:number,
        y:number,
    }

    // 返回位置,旋转,缩放
    export function unpackTransformByNumberArray(numberArray:number[]):[Vector,Rotation,Vector]{
        if(!numberArray || numberArray.length != 9){
            console.error(`传入要转换的数组无法转换为transform,:${numberArray}`);
            return [Vector.zero,Rotation.zero,Vector.zero]
        }
        return [
            new mw.Vector(numberArray[0],numberArray[1],numberArray[2]),
            new mw.Rotation(numberArray[3],numberArray[4],numberArray[5]),
            new mw.Vector(numberArray[6],numberArray[7],numberArray[8]),
        ]
    }

    export function vector2World(vector: Vector3Like, worldPosition: Vector3Like, forward: Vector3Like, right: Vector3Like): mw.Vector {
        return new mw.Vector(
            vector.x * forward.x + vector.y * right.x + worldPosition.x,
            vector.x * forward.y + vector.y * right.y + worldPosition.y,
            vector.x * forward.z + vector.y * right.z + vector.z + worldPosition.z,
        )
    }

    export function getCirclePosition(radius:number, count:number, index:number):Vector{
        let x = index / count * 6.283
        return new Vector(
            Math.sin(x) * radius,
            Math.cos(x) * radius,
            0
        )
    }

    // 获取随机姓名
    export function getRandomName():string{
        return Constants.RobotFamilyNameArrayCN[Math.floor((Math.random() * Constants.RobotFamilyNameArrayCN.length))]
    }

    export function randomNegative():number{
        return Math.random() * 2 - 1
    }

    export function lerp(a:number,b:number,t:number):number{
        return (b - a) * t + a
    }

    export function clamp(value:number, min:number, max:number):number{
        return Math.min(Math.max(value, min), max)
    }

    export function colorLerp(a:mw.LinearColor,b:mw.LinearColor,t:number):mw.LinearColor{
        return new mw.LinearColor(
            lerp(a.r,b.r,t),
            lerp(a.g,b.g,t),
            lerp(a.b,b.b,t)
        )
    }

    // 根据秒数转换成MM:SS
    export function toMM_SSText(second: number): string {
        let m = Math.floor(second / 60)
        let s = Math.floor(second % 60)
        return `${m < 10 ? `0${m}` : m}:${s < 10 ? `0${s}` : s}`
    }
    
    // 获取资源库Icon赋值到目标ImageUI
    export function loadAssetsIcon(guid: string, targetImage: mw.Image) {
        // 如果本地有
        if (mw.getAssetIconDataByAssetID(guid)) {
            targetImage.setImageByAssetIconData(getAssetIconDataByAssetID(guid));
            return
        }
        // 本地没有
        mw.assetIDChangeIconUrlRequest([guid]).then(()=>{
            let iconData = mw.getAssetIconDataByAssetID(guid);
            if (!iconData) return;
            targetImage.setImageByAssetIconData(iconData);
        })
    }
    
    // [0-1]
    export function pingPong(t: number): number {
        return Math.abs(t % 1 - 0.5) * 2
    }

    // 大数转缩写文本
    export function getBigNum(num: number): string {
        let str = "";
        if (num <= 9999) {
            str = Math.round(num) + "";
        } else if (num > 9999 && num <= 999999) {
            str = Math.floor(num / 1000) + "K";
        } else {
            str = (num / 1000000).toFixed(0) + "M";
        }
        return str;
    }

    // 将数组内指定元素替换至0
    export function arrayElementReplaceToMinusOne(array:number[], targetValue:number){
        for(let i = 0;i < array.length;i++){
            if(array[i] != targetValue) continue
            array[i] = 0
            return
        }
    }

    // 将数组全部元素替换至0
    export function arrayAllElementReplaceToMinusOne(array:number[]){
        for(let i = 0;i < array.length;i++){
            array[i] = 0
        }
    }

    // 填充数据到指定数组(如果有0则替换0)
    export function arrayPushElement(array:number[], value:number){
        for(let i = 0;i < array.length;i++){
            if(array[i] != 0) continue
            array[i] = value
            return
        }
        array.push(value)
    }

    // 获取数组中不等于0的元素数量
    export function arrayGetCount(array:number[]):number{
        let count = 0
        for (const id of array) {
            if(id != 0) count++
        }
        return count
    }

    // 获取临时color
    let __temporary_color = LinearColor.black
    export function getTemporaryLinearColor(r:number, g:number, b:number):LinearColor{
        __temporary_color.r = r
        __temporary_color.g = g
        __temporary_color.b = b
        return __temporary_color
    }

    // 获取临时vector3
    let __temporary_vector = Vector.zero
    export function getTemporaryVector(x:number, y:number, z:number):Vector{
        __temporary_vector.x = x
        __temporary_vector.y = y
        __temporary_vector.z = z
        return __temporary_vector
    }

    /**
     * 使用资源ID加载资源图到Image上
     * @param image   图片
     * @param assetsId  资源ID
     */
    export function assetsIconInImage(image: mw.Image, assetsId: string) {
        if (mw.getAssetIconDataByAssetID(assetsId)) {
            image.setImageByAssetIconData(mw.getAssetIconDataByAssetID(assetsId));
        } else {
            mw.assetIDChangeIconUrlRequest([assetsId]).then(() => {
                if (mw.getAssetIconDataByAssetID(assetsId)) {
                    image.setImageByAssetIconData(mw.getAssetIconDataByAssetID(assetsId));
                }
            });
        }
    }

    // 获取今天是今年的第几天
    export function getTodayOfYear():number{
        return Math.ceil(( Number(new Date()) - Number(new Date(new Date().getFullYear().toString())))/(24*60*60*1000)) + 1
    }
    
    export function getBigNumberText(num:number):string{
        if(num < 1000) return num.toString()
        if(num < 10000) return `${(num / 1000).toFixed(2)}k`

        return `${(num / 10000).toFixed(2)}w`
    }

    // 字体大小:34
    // 组件宽:340
    // 汉字:7个 单个汉字占:49
    // 数字:13个 单个数字占:27
    // 字母:13个 单个字母占:27
    export function getStringInUIWidth(fontSize:number, context:string):number{
        let width = 0

        for(let i = 0;i < context.length;i++){
            let code = context.charCodeAt(i)
            // 空格
            if(code == 32){
                width += fontSize * 0.4
            }
            // 汉字
            else if(code > 255){
                width += fontSize * 1.44
            }
            // 字母&数字
            else{
                width += fontSize * 0.8
            }
        }
        
        return width
    }
}